package com.chuisha.stu.controller;

import com.chuisha.common.CommonResult;
import com.chuisha.stu.service.ProductService;
import com.chuisha.stu.service.vo.ProductVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Log4j2
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/product/{productCode}")
    public CommonResult<ProductVO> getByCode(@PathVariable String productCode) {
        log.info("get product detail,productCode is :{}", productCode);
        return CommonResult.success(productService.selectByCode(productCode));
    }

    @PostMapping("/product/update")
    public CommonResult<ProductVO> update(ProductVO productVO) {
        log.info("update product:{}", productVO);
        return CommonResult.success(productService.updateProduct(productVO));
    }

    @PostMapping("/product/insert")
    public CommonResult<ProductVO> insert(ProductVO productVO) {
        log.info("insert product:{}", productVO);
        return CommonResult.success(productService.insertProduct(productVO));
    }

    @PostMapping("/product/delete")
    public CommonResult<Integer> delete(@RequestParam String productCode) {
        log.info("delete product,productCode is {}", productCode);
        return CommonResult.success(productService.deleteProduct(productCode));
    }
}